package com.anytron.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.anytron.model.AppNode;
import com.anytron.model.req.PageRequest;
import com.anytron.model.res.AppNodePOJO;
import com.anytron.model.res.ResultData;
import com.anytron.service.AppInfoService;
import com.anytron.service.AppNodeService;
import com.anytron.service.NodeInfoService;
import com.github.pagehelper.PageInfo;


@RestController
@RequestMapping(value = "app/node")
public class AppNodeController {
	
	@Autowired
	AppInfoService appInfoService;
	@Autowired
	NodeInfoService nodeInfoService;
	@Autowired
	AppNodeService appNodeService;
	
	@RequestMapping(value = "list")
	public ResultData<PageInfo<AppNodePOJO>> list(PageRequest pageRequest,AppNode appNode) throws Exception {
		ResultData<PageInfo<AppNodePOJO>> resultData = new ResultData<>();
		PageInfo<AppNodePOJO> pageInfo = appNodeService.selectAllAppNodeInfoByPage(pageRequest, appNode);
		resultData.setData(pageInfo);
		resultData.setErrorCode(0);
		resultData.setSuccess(true);
		return resultData;
	}
	
	@RequestMapping(value = "getAppNodeBySomeId")
	public ResultData<List<AppNodePOJO>> getAppNodeBySomeId(@RequestBody AppNode appNode) throws Exception {
		ResultData<List<AppNodePOJO>> resultData = new ResultData<>();
		List<AppNodePOJO> appNodeInfo = appNodeService.selectAllAppNodeInfo(appNode);
		resultData.setData(appNodeInfo);
		resultData.setErrorCode(0);
		resultData.setSuccess(true);
		return resultData;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "saveOrEdit" ,method = RequestMethod.POST)
	public ResultData<String> saveOrEdit(@RequestBody Map<String , Object> paramMap) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		Long appId = Long.parseLong(paramMap.get("appId").toString());
		AppNode deleteAppNode = new AppNode();
		deleteAppNode.setAppId(appId);
		appNodeService.deleteByModel(deleteAppNode);
		List<Object> list = (List) paramMap.get("nodes");
		if (!CollectionUtils.isEmpty(list)) {
			list.forEach(item->{
				Map<String,Object> dataMap = (Map<String, Object>) item;
				Long nodeId = Long.parseLong(dataMap.get("value").toString());
				AppNode appNode = new AppNode();
				appNode.setAppId(appId);
				appNode.setNodeId(nodeId);
				appNodeService.add(appNode);
			});
		}
		resultData.setErrorCode(0);
		resultData.setSuccess(true);
		return resultData;
	}
	
	@RequestMapping(value = "update")
	public ResultData<String> update(@RequestBody AppNode appNode) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		int res = appNodeService.update(appNode);
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("更新失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
	
	@RequestMapping(value = "delete")
	public ResultData<String> delete(@RequestBody AppNode appNode) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		int res = appNodeService.delete(appNode.getId());
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("删除失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
}
